/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#ifndef __HTTPACSINTERFACE_H__
#define __HTTPACSINTERFACE_H__

#include <map>
#include <fstream>
#include <iostream>
#include <tinyxml.h>
#include <rgcpp/tools/ToHex.h>
#include <boost/filesystem.hpp>
#include <rgcpp/structs/Types.h>
#include <rgcpp/tools/ToString.h>
#include <pion/net/HTTPTypes.hpp>
#include <pion/net/WebService.hpp>
#include <rgcpp/structs/Sequence.h>
#include <rgcpp/structs/FileLock.h>
#include <rgcpp/net/http/MIMETypes.h>
#include <rgcpp/tools/SHAGenerator.h>
#include <boost/algorithm/string.hpp>
#include <pion/net/HTTPResponseWriter.hpp>
#include <rgcpp/exceptions/RuntimeException.h>

#include "data/Device.h"

namespace acs
{

#define COOKIE_DEVICE_ID "DeviceID"
#define COOKIE_DEVICE_1ST_CONFIG "Device1stConfig"
#define COOKIE_DEVICE_LAST_UPDATE "DeviceUpdate"
#define COOKIE_DEVICE_LAST_RESTORE "DeviceRestore"
#define COOKIE_DEVICE_BACKUP_MODE "DeviceBackupMode"
#define COOKIE_COMMAND_INDEX "DeviceCommandIndex"

class HttpACSInterface : public pion::net::WebService
{
	private:
		static unsigned int seqID;
		static std::string TMP_FILE_PEFIX;
		static rgcpp::Sequence<int> fileID;

	public:
		HttpACSInterface();
		~HttpACSInterface();
		virtual void operator()(pion::net::HTTPRequestPtr& request, pion::net::TCPConnectionPtr& tcp_conn);

	private:
		void handlerPostRequest(pion::net::HTTPRequestPtr& request, pion::net::TCPConnectionPtr& tcpConn);
		std::string handlerPostRequestInform(pion::net::HTTPRequestPtr& request, pion::net::HTTPResponseWriterPtr& writer, const TiXmlElement* message, const TiXmlElement* command, const std::string& name, const std::string& license, const std::string& ip);
		std::string handlerPostRequestBackup(pion::net::HTTPRequestPtr& request, pion::net::HTTPResponseWriterPtr& writer, const TiXmlElement* message, const TiXmlElement* command, const std::string& name, const std::string& license, const std::string& ip);
		std::string handlerPostRequestRestore(pion::net::HTTPRequestPtr& request, pion::net::HTTPResponseWriterPtr& writer, const TiXmlElement* message, const TiXmlElement* command, const std::string& name, const std::string& license, const std::string& ip);
		std::string handlerPostRequestCommand(pion::net::HTTPRequestPtr& request, pion::net::HTTPResponseWriterPtr& writer, const TiXmlElement* message, const TiXmlElement* command, const std::string& name, const std::string& license, const std::string& ip);
		std::string handlerPostRequestBulkCommands(pion::net::HTTPRequestPtr& request, pion::net::HTTPResponseWriterPtr& writer, const TiXmlElement* message, const TiXmlElement* command, const std::string& name, const std::string& license, const std::string& ip);

		std::string buildMessageSynchronized(const std::string& sha);
		std::string buildMessageError(const std::string& error);

		std::string buildMessage(const Command& action);
		std::string buildMessage(const std::vector<Command>& actions);

		std::string getDeviceSHA(const int id);
};

}

#endif
